package com.xianggu.controller;

import com.alibaba.druid.util.StringUtils;
import com.plugin.MyPlugin;
import com.xianggu.common.XiangGuService;
import com.xianggu.demo.dto.UserDto;
import com.xianggu.mybatis.paging.Page;
import com.xianggu.spring.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chengjy
 * @date 2025/3/9
 */
@RequestMapping("/users")
@Controller
public class AdminSystemController {

    MyPlugin myPlugin;

    @AutoWired
    private XiangGuService xiangGuService;

        @GetMapping("/searchContidion")
    public Page<UserDto> getUserInfo(@RequestParam Map<String,Object> param) {
        if(myPlugin != null){
            myPlugin.execute();
        }

        if (!param.containsKey("pageNum")) {
            param.put("pageNum", 1);
        }
        if (!param.containsKey("pageSize")) {
            param.put("pageSize", 10);
        }
        return xiangGuService.searchContidion(param);
    }
    @GetMapping("/list")
    public Page<UserDto> getUserInfo() {
        if(myPlugin != null){
            myPlugin.execute();
        }
        int pageNum = 1;
        int pageSize = 10;
        return xiangGuService.getInitList(pageNum , pageSize);
    }
    @GetMapping("/getById/{id}")
    public UserDto getById(@PathVariable("id") String id)  {
        if(myPlugin != null){
            myPlugin.execute();
        }
        int pageNum = 1;
        int pageSize = 10;
        return xiangGuService.getUserById(id);
    }
    public String deleteById(@PathVariable("id") String id)  {
        if(myPlugin != null){
            myPlugin.execute();
        }
        int pageNum = 1;
        int pageSize = 10;
        return xiangGuService.deleteById(id);
    }
    public String updateById(@RequestBody UserDto userDto)  {
        if(myPlugin != null){
            myPlugin.execute();
        }
        int pageNum = 1;
        int pageSize = 10;
        return xiangGuService.updateById(userDto);
    }
    @GetMapping("/getUserListMap")
    public List<Map<String,Object>> getUserListMap(@RequestParam("id") String id , @RequestParam("pageNum") int pageNum , @RequestParam("pageSize") int pageSize) {
        if(myPlugin != null){
            myPlugin.execute();
        }
        return xiangGuService.getUserListMap(id , pageNum , pageSize);
    }
    public String save(@RequestBody UserDto userDto) {
        if(myPlugin != null){
            myPlugin.execute();
        }
        if(StringUtils.isEmpty(userDto.getId())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            userDto.setId(sdf.format(new Date()));
        }
        System.out.println("执行新增数据："+ userDto);
        return xiangGuService.saveUser(userDto);
    }
}
